package cn.gailvlun.gll.chatlib.message;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import cn.gailvlun.gll.net.apartment.room.Room;
import cn.gailvlun.gll.net.apartment.task.TaskRecord;
import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.util.AudioUtil;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.ProfileUtil;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.Required;



public class Message extends RealmObject {
    @PrimaryKey
    private String id = UUID.randomUUID().toString(); 
    private Profile sender; 
    @Required
    private Integer type; 
    @Required
    private String dialogId; 
    private String text; 
    private String image; 
    private String walkieTalkie; 
    @Required
    private Date created; 
    private Integer width; 
    private Integer height; 
    private Integer wtSecond; 
    private boolean isWTPlayed; 
    private boolean unread = true; 
    private Room room; 
    private TaskRecord taskRecord;

    private String localWalkieTalkie;
    private String localImage;
    private boolean atAll;
    private RealmList<Integer> atWhomList;

    private int messageStatus = MessageStatus.SUCCESS.getValue();
    private String nickName;

    public static Message text(String text, Date created, Profile sender, String dialogId) {
        Message msg = new Message();
        msg.setType(MessageType.text);
        msg.setText(text);
        msg.setCreated(created);
        msg.setSender(sender);
        msg.setDialogId(dialogId);
        return msg;
    }

    public static Message image(String image, Date created, Profile sender, String dialogId) {
        Message imgMsg = new Message();
        imgMsg.setType(MessageType.image);
        imgMsg.setImage(image);
        imgMsg.setCreated(created);
        imgMsg.setSender(sender);
        imgMsg.setDialogId(dialogId);
        return imgMsg;
    }

    public static Message localWalkieTalkie(String localWalkieTalkie, Date created, Profile sender, String dialogId) {
        Message imgMsg = new Message();
        imgMsg.setType(MessageType.walkieTalkie);
        imgMsg.setLocalWalkieTalkie(localWalkieTalkie);
        imgMsg.setWtSecond((int) (AudioUtil.getDuration(localWalkieTalkie)) / 1000);
        imgMsg.setWTPlayed(false);
        imgMsg.setCreated(created);
        imgMsg.setSender(sender);
        imgMsg.setDialogId(dialogId);
        return imgMsg;
    }

    public static Message checkIn(TaskRecord taskRecord, Date created, Profile sender, String dialogId) {
        Message msg = new Message();
        msg.setType(MessageType.checkIn);
        msg.setTaskRecord(taskRecord);
        msg.setCreated(created);
        msg.setSender(sender);
        msg.setDialogId(dialogId);
        return msg;
    }

    public static Message invite(Room room, Date created, Profile sender, String dialogId) {
        Message roomInviteMsg = new Message();
        roomInviteMsg.setType(MessageType.invite);
        roomInviteMsg.setText("我邀请你参加" + room.getName() + "房间");
        roomInviteMsg.setSender(sender);
        roomInviteMsg.setCreated(created);
        roomInviteMsg.setDialogId(dialogId);
        roomInviteMsg.setRoom(room);
        return roomInviteMsg;
    }

    public static Message atWhomText(String text, Date created, Profile sender, String dialogId, boolean atAll, List<Integer> atWhomList) {
        Message msg = new Message();
        msg.setType(MessageType.text);
        msg.setText(text);
        msg.setCreated(created);
        msg.setSender(sender);
        msg.setDialogId(dialogId);
        msg.setAtAll(atAll);
        if (!IsEmpty.list(atWhomList)) {
            msg.setAtWhomList(atWhomList);
        }
        return msg;
    }

    public static Message sticker(String localStickerPath, Date created, Profile sender, String dialogId) {
        Message imgMsg = new Message();
        imgMsg.setType(MessageType.sticker);
        imgMsg.setLocalImage(localStickerPath);
        imgMsg.setCreated(created);
        imgMsg.setSender(sender);
        imgMsg.setDialogId(dialogId);
        return imgMsg;
    }

    public Integer getUserOrRoomId() {
        return Integer.valueOf(dialogId.split(":")[1]);
    }

    public boolean isMe() {
        if (this.getSender().getId() == ProfileUtil.getUserId()) {
            return true;
        }
        return false;
    }

    public boolean isRoom() {
        if (this.getDialogId().split(":")[0].equals("user")) {
            return false;
        }
        return true;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Profile getSender() {
        return sender;
    }

    public void setSender(Profile sender) {
        this.sender = sender;
    }

    public MessageType getType() {
        return MessageType.getByInt(type);
    }

    public void setType(MessageType type) {
        this.type = type.getMsgValue();
    }

    public String getDialogId() {
        return dialogId;
    }

    public void setDialogId(String dialogId) {
        this.dialogId = dialogId;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getWalkieTalkie() {
        return walkieTalkie;
    }

    public void setWalkieTalkie(String walkieTalkie) {
        this.walkieTalkie = walkieTalkie;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public Integer getWtSecond() {
        return wtSecond;
    }

    public void setWtSecond(Integer wtSecond) {
        this.wtSecond = wtSecond;
    }

    public boolean getWTPlayed() {
        return isWTPlayed;
    }

    public void setWTPlayed(boolean WTPlayed) {
        isWTPlayed = WTPlayed;
    }

    public boolean isUnread() {
        return unread;
    }

    public void setUnread(boolean unread) {
        this.unread = unread;
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }

    public TaskRecord getTaskRecord() {
        return taskRecord;
    }

    public void setTaskRecord(TaskRecord taskRecord) {
        this.taskRecord = taskRecord;
    }

    public String getLocalWalkieTalkie() {
        return localWalkieTalkie;
    }

    public void setLocalWalkieTalkie(String localWalkieTalkie) {
        this.localWalkieTalkie = localWalkieTalkie;
    }

    public String getLocalImage() {
        return localImage;
    }

    public void setLocalImage(String localImage) {
        this.localImage = localImage;
    }

    public String getRealWalkieTalkie() {
        if (IsEmpty.string(localWalkieTalkie)) {
            return walkieTalkie;
        }
        return localWalkieTalkie;
    }

    public String getRealImage() {
        if (IsEmpty.string(localImage)) {
            return image;
        }
        return localImage;
    }

    public MessageStatus getMessageStatus() {
        return MessageStatus.getMessageStatusByValue(this.messageStatus);
    }

    public void setMessageStatus(MessageStatus messageStatus) {
        this.messageStatus = messageStatus.getValue();
    }

    public boolean isAtAll() {
        return atAll;
    }

    public void setAtAll(boolean atAll) {
        this.atAll = atAll;
    }

    public List<Integer> getAtWhomList() {
        return atWhomList;
    }

    public void setAtWhomList(List<Integer> atWhomList) {
        if (this.atWhomList == null){
            this.atWhomList=new RealmList<>();
        }
        this.atWhomList.addAll(atWhomList);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Message)) {
            return false;
        }
        return this.id.equals(((Message) obj).id);
    }

    @Override
    public int hashCode() {
        return this.id.hashCode();
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}
